package com.zyx.crawlerdemo.httpclient.crawler;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * @author Yaxi.Zhang
 * @since 2021/8/11 17:13
 * desc: httpclient的超时设置案例1
 */
public class HttpclientTimeout1 {

	public static void main(String[] args) throws ClientProtocolException, IOException {
		//全部设置为10秒
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(10000)
				.setConnectTimeout(10000)
				.setConnectionRequestTimeout(10000)
				.build();
		// 配置httpClient
		HttpClient httpClient = HttpClients.custom()
				.setDefaultRequestConfig(requestConfig)
				.build();
		HttpGet httpGet = new HttpGet("https://searchcustomerexperience.techtarget.com/info/news");
		HttpResponse response = null;  
		try {
			// 执行请求
			response = httpClient.execute(httpGet);
		}catch (Exception e){  
			e.printStackTrace();  
		}
		// 获取结果，html
		String result = EntityUtils.toString(response.getEntity(),"gbk");
		// 输出结果
		System.out.println(result);
	}
}
